﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using WLFramework.Json;

namespace GenericHtml
{
    /// <summary>
    /// 页面读取类
    /// </summary>
    public class ReadHtml : ILoginInfo
    {
        /// <summary>
        /// 当前页面的表
        /// </summary>
        public DynamicTable Table { get; private set; }

        public string TableName { get; private set; }

        /// <summary>
        /// 当前登录用户
        /// </summary>
        public UserInfo LoginUser { get; set; }

        /// <summary>
        /// 初始化页面读取类
        /// </summary>
        /// <param name="table"></param>
        public ReadHtml(string table)
        {
            this.TableName = table;
            this.Table = MetadataManager.GetTable(table);
            if (this.Table == null)
                throw new ArgumentNullException("table");
        }

        public void Read(HttpContext context)
        {
            this.LoginUser = context.Session["current_user"] as UserInfo;
            if (this.LoginUser == null)
            {
                this.WriteParentRediret(context, "/admin/login.aspx");
                return;
            }

            context.Items["User"] = this.LoginUser;
            context.Items["Table"] = this.Table;

            context.Server.Transfer("~/template/default.aspx");
        }

        public void WriteParentRediret(HttpContext context, string url)
        {
            context.Response.Write(string.Format(
@"<script type=""text/javascript"">
    if(top != this)
        parent.location.href='{0}';
    else
        document.location.href = '{0}';
</script>",
                    url));
        }
    }
}